#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>

// 终止指定名称的进程
BOOL TerminateProcessByName(const char* processName) {
    HANDLE hProcessSnap;
    PROCESSENTRY32 pe32;

    // 创建系统进程快照
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcessSnap == INVALID_HANDLE_VALUE) {
        return FALSE;
    }

    // 设置 PROCESSENTRY32 结构体的大小
    pe32.dwSize = sizeof(PROCESSENTRY32);

    // 获取第一个进程的信息
    if (!Process32First(hProcessSnap, &pe32)) {
        CloseHandle(hProcessSnap);
        return FALSE;
    }

    // 遍历所有进程
    do {
        if (strcmp(pe32.szExeFile, processName) == 0) {
            HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
            if (hProcess != NULL) {
                // 终止进程
                if (TerminateProcess(hProcess, 0)) {
                    CloseHandle(hProcess);
                    CloseHandle(hProcessSnap);
                    return TRUE;
                }
                CloseHandle(hProcess);
            }
        }
    } while (Process32Next(hProcessSnap, &pe32));

    CloseHandle(hProcessSnap);
    return FALSE;
}

int main() {
    const char* processName = "slui.exe";
    while (1) {
        if (TerminateProcessByName(processName)) {
            printf("slui.exe 进程已终止。\n");
        }
        Sleep(1000); // 每秒检查一次
    }
    return 0;
}    